﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class spawner : MonoBehaviour {
	public GameObject Prefab;
	public Texture2D Crosshair;
	public string LastDir = "C:\\";
	public string CurDir = "C:\\";
	public string[] DirDir;
	public string[] DirFiles;
	public float CntWidth = 10;
	public float Size = 1.5f;
	public List<GameObject> Spawned;
	public DriveInfo[] Drives;

	void Start () {
		//Drives = DriveInfo.GetDrives();

	}
	void OnGUI(){
		if(Input.GetMouseButton(1)){
			GUI.DrawTexture(new Rect(Screen.width/2-2,Screen.height/2-2,4,4),Crosshair);
		}


	}	
	void Update () {
		if(Input.GetMouseButtonDown(0)){
			GameObject tempGO = FireRaycastGetThingInFrontOfMe(Camera.main.gameObject);
			if(tempGO){
				if(tempGO.GetComponent<Box>()){
					if(tempGO.GetComponent<Box>().IsDir){
						LastDir = tempGO.GetComponent<Box>().StrPath+"\\";
						CurDir = tempGO.GetComponent<Box>().StrPath;
						UpdateBoxes();
					}
					else{
						//its a file so parse it and maybe rerender it
						this.GetComponent<render>().ParseExtOnce(tempGO.GetComponent<Box>().StrPath,tempGO);
					}

				}
			}
		}
		if(Input.GetKeyDown(KeyCode.C)){
			CurDir = "C:\\";
			LastDir = CurDir;
			UpdateBoxes();

		}
		if(Input.GetKeyDown(KeyCode.U)){
			CurDir = LastDir;
			UpdateBoxes();
		}
		
		



	}
	public GameObject FireRaycastGetThingInFrontOfMe(GameObject Me){
		Ray FireRay = new Ray(Me.transform.position,Me.transform.forward);
		RaycastHit FireRayHit;
		if(Physics.Raycast(FireRay, out FireRayHit , this.transform.GetComponent<render>().Range)){
			print("HIT:"+FireRayHit.collider.transform.gameObject.name);
			if(FireRayHit.collider.gameObject.name == "RenderPlane"){
				return FireRayHit.collider.transform.parent.parent.gameObject;
			}
		}
		return null;
	}
	public void UpdateBoxes(){
		//since were changing boxes lets clean the arrays of some of them
		this.transform.GetComponent<render>().NewActive =  new List<GameObject>();
		this.transform.GetComponent<render>().OldActive =  new GameObject[0];
		
		
		int Cnt = 0;
		foreach(GameObject GO in Spawned){
			Cnt++;
			Destroy(GO);
		}
		Spawned.RemoveRange(0,Cnt);

		DirDir = Directory.GetDirectories(CurDir);
		DirFiles = Directory.GetFiles(CurDir);
		CntWidth = Mathf.Sqrt(DirFiles.Length + DirDir.Length);


		int x = 0;
		int y = 0;
		foreach(string S in DirDir){
			SpawnObj(S,new Vector2(x,y), true);
			x++;
			if(x >= CntWidth){
				x = 0;
				y++;
			}
		}
		foreach(string S in DirFiles){
			SpawnObj(S,new Vector2(x,y), false);
			x++;
			if(x >= CntWidth){
				x = 0;
				y++;
			}
		}
	}
	public void SpawnObj(string StrInput, Vector2 Loc, bool IsDir){
		GameObject GO = (GameObject)Instantiate(Prefab,new Vector3(Loc.x*Size,0,Loc.y*Size),Quaternion.identity);
		if(IsDir){
			GO.name = "Directory ["+StrInput+"]";
			GO.transform.GetChild(1).transform.GetChild(0).renderer.material.mainTexture = GO.GetComponent<Box>().DefaultFolder;
			GO.GetComponent<Box>().IsDir = true;
			GO.GetComponent<Box>().StrPath = StrInput;
			string Tmp = StrInput.Substring(LastDir.Length);
			GO.transform.GetChild(0).name = Tmp;
			GO.transform.GetChild(0).GetComponent<TextMesh>().text = Tmp;
		}
		else{
			GO.name = "File ["+StrInput+"]";
			GO.transform.GetChild(1).transform.GetChild(0).renderer.material.mainTexture = GO.GetComponent<Box>().DefaultFile;
			GO.GetComponent<Box>().IsDir = false;
			GO.GetComponent<Box>().StrPath = StrInput;
			string Tmp = StrInput.Substring(LastDir.Length);
			GO.transform.GetChild(0).name = Tmp;
			GO.transform.GetChild(0).GetComponent<TextMesh>().text = Tmp;
		}
		//GO.transform.GetChild(0).transform.localPosition = new Vector3(0,0,-(Size/Size));
		//GO.transform.GetChild(1).transform.localScale = new Vector3(Size,Size,Size);

		Spawned.Add(GO);
	}




}
